import { IObject } from '@/utils/constants'

/* 将新的样式插入头部head style中 */
export const writeNewStyle = (originalStyle: string, colors: IObject): void => {
  Object.keys(colors).forEach(key => {
    /* 根据模板将之前的变量标记替换为颜色值 */
    const reg = new RegExp('(:|\\s+)' + key, 'g')
    originalStyle = originalStyle.replace(reg, '$1' + colors[key])
  })

  /* 如果有id为chalk-theme-style的话 可以直接修改内容 反之进行创建 */
  const chalkStyle = document.getElementById('chalk-theme-style')
  if ( !chalkStyle ) {
    const style = document.createElement('style')
    style.innerText = originalStyle
    style.id = 'chalk-theme-style'
    document.head.appendChild(style)
  } else {
    (chalkStyle as HTMLElement).innerText = originalStyle
  }
}
